﻿package treatBehavior
{
	import flash.geom.Point;
	
	public class ResortBehavior implements ITreatBehavior
	{
													//----------------------------------------------API------------------------------------
													//================ Properties ===============
													
													//================ Constants ================

													//================ Constructor ==============
		public function ResortBehavior(aVal:Array,dVal:Number,nVal:uint):void
		{
			arr=aVal;
			d=dVal;
			n=nVal;
			init();
		}													
													//================ Public Methods ===========
		public function getTreatArr():Array
		{
			var treatedArr:Array=new Array();
			var diet:Number=d/n;
			var i:uint=1;
			var m:uint=0;//treatedArr的移动点；
			treatedArr.push(arr[0]);//加入第一个点；
			try
			{
				wholeloop:while(i<arr.length)
				{
					var distance:Number=Point.distance(treatedArr[m],arr[i]);
					if(distance==diet)
					{
						treatedArr.push(arr[i]);
						//trace(Point.distance(treatedArr[m],treatedArr[m+1]));
						m++;
						i++;
						continue wholeloop;
					}
					if(distance>diet)
					{
						var radio:Number=(distance-diet)/distance;
						treatedArr.push(Point.interpolate(treatedArr[m],arr[i],radio));
						//trace(Point.distance(treatedArr[m],treatedArr[m+1]));
						m++;
						continue wholeloop;
					}
					if(distance<diet)
					{
						for(var k:int=i;k<arr.length-1;k++)//miniloop只是让原始数组的索引往后走，找到大于指定距离的点；在这里要加入的点在arr[i-1]与arr[i]连线之间。
						{
							i++;
							var tempDistance:Number=Point.distance(treatedArr[m],arr[i]);
							if(tempDistance==diet)
							{
								treatedArr.push(arr[i]);
								m++;
								i++;
								continue wholeloop;
								break;				
							}
							if(tempDistance>diet)
							{
								break;
							}
						}
						if(Point.distance(treatedArr[m],arr[i])<diet)break wholeloop;
						for(var x:uint=0;;x++)
						{
							if((Point.distance(arr[i-1],arr[i])-x*diet/50)>=0)
							{
								var r:Number=(Point.distance(arr[i-1],arr[i])-x*diet/50)/Point.distance(arr[i-1],arr[i]);
								var tempPoint:Point=Point.interpolate(arr[i-1],arr[i],r);
								if(Point.distance(treatedArr[m],tempPoint)>=diet)
								{
									treatedArr.push(tempPoint);
									//trace("step and insert");
									//trace(Point.distance(treatedArr[m],treatedArr[m+1]));
									m++;
									continue wholeloop;
								}
							}else{
								treatedArr.push(arr[i]);
								//trace("step!!");
								//trace(Point.distance(treatedArr[m],treatedArr[m+1]));
								m++;
								i++;							
								break;
							}
						}				
					}				
				}
			}
			catch(error:Error)
			{
				//trace(error);
				//trace(i,m,arr.length);
			}	
			return treatedArr;
		}
													//----------------------------------------------private--------------------------------													
													//================ Properties ===============
		private var arr:Array;
		private var d:Number;
		private var n:uint;
													//================ Private Methods ==========
		private function init():void
		{
		}
													//================ Destroy ==================		
	}
}